﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using A2DFramework.CacheService.Configuration;
using A2DFramework.CommandBusService;
using A2DFramework.CommandBusService.MSMQCommandBus;
using A2DFramework.CommandBusService.MSMQCommandBus.Configuration;
using A2DFramework.CommandBusService.NativeCommandBus;
using A2DFramework.CommandBusService.NativeCommandBus.Configuration;
using A2DFramework.CompressionService.Configuration;
using A2DFramework.EventService.Configuration;
using A2DFramework.InstanceService;
using A2DFramework.InstanceService.Configuration;
using A2DFramework.LogService.Configuration;
using A2DFramework.SecurityService.Configuration;
using A2DFramework.SessionService.Configuration;
using A2DFramework.SQLDispatcherService.Configuration;

namespace A2DFramework.A2DConfiguration
{
    public static class A2DConfig
    {
        public static void LoadConfig()
        {
            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));
            if (root.Element("CacheStorages") != null)
                CacheConfiguration.Load();

            if (root.Element("CommandBusSetting") != null)
            {
                if (root.Element("CommandBusSetting").Attribute("BusType").Value == "Native")
                {
                    NativeCommandBusConfiguration.Load();
                    ObjectFactory.Register<ICommandBus, NativeCommandBusImpl>();
                }
                if (root.Element("CommandBusSetting").Attribute("BusType").Value == "MSMQ")
                {
                    MSMQCommandBusConfiguration.Load();
                    ObjectFactory.Register<ICommandBus, MSMQCommandBusImpl>();
                }
            }

            if (root.Element("InstanceConfig") != null)
                InstanceConfiguration.Load();

            if (root.Element("EventSetting") != null)
                EventServiceConfiguration.Load();

            if (root.Element("MongoDBSession") != null)
                SessionConfiguration.Load();

            if (root.Element("SQLDispatcher") != null)
                SQLDispatcherConfiguration.Load();

            if (root.Element("LogConfig") != null)
                LogConfiguration.Load();

            if (root.Element("SecurityConfig") != null)
                SecurityConfiguration.Load();

            CompressionConfiguration.Load();
        }
    }
}
